package cn.kbyue.exceptiondemo.controller;

import cn.kbyue.exceptiondemo.common.CommonResponse;
import cn.kbyue.exceptiondemo.common.QueryData;
import cn.kbyue.exceptiondemo.common.QueryDataResponse;
import cn.kbyue.exceptiondemo.common.validate.Save;
import cn.kbyue.exceptiondemo.common.validate.Update;
import cn.kbyue.exceptiondemo.common.validate.ValidationList;
import cn.kbyue.exceptiondemo.dto.LicenceDTO;
import cn.kbyue.exceptiondemo.dto.LicenceParam;
import cn.kbyue.exceptiondemo.entity.Licence;
import cn.kbyue.exceptiondemo.service.LicenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.groups.Default;
import java.util.List;

@RestController
@RequestMapping("/licence")
public class LicenceController {

    @Autowired
    private LicenceService licenceService;

    @GetMapping("/{licenceId}")
    public CommonResponse<Licence> getLicence(@PathVariable("licenceId") Long licenceId) {
        Licence licence = licenceService.queryDetail(licenceId);
        return CommonResponse.success(licence);
    }

    @GetMapping("/list")
    public QueryDataResponse<Licence> getLicences(@Validated LicenceParam licenceParam) {
        QueryData<Licence> licences = licenceService.getLicences(licenceParam);
        return QueryDataResponse.success(licences);
    }

    /**
     * 方法参数上. 注解 @Validated 和 @Valid 都可以生效
     * @param licenceDTO dto
     * @return resp
     */
    @PostMapping("/validate")
    public CommonResponse<LicenceDTO> validate(@RequestBody @Valid LicenceDTO licenceDTO) {
        return CommonResponse.success(licenceDTO);
    }

    /**
     * 参数校验, 字段校验分组为 Save 的规则
     * @param licenceDTO dto
     * @return resp
     */
    @PostMapping("/save")
    public CommonResponse<LicenceDTO> save(@RequestBody @Validated(Save.class) LicenceDTO licenceDTO) {
        return CommonResponse.success(licenceDTO);
    }

    /**
     * 参数校验, 字段校验分组为 Update 的规则
     * @param licenceDTO dto
     * @return resp
     */
    @PostMapping("/update")
    public CommonResponse<LicenceDTO> update(@RequestBody @Validated(Update.class) LicenceDTO licenceDTO) {
        return CommonResponse.success(licenceDTO);
    }

    /**
     * 参数校验, 字段校验分组为 Update + Default 的规则
     * @param licenceDTO dto
     * @return resp
     */
    @PostMapping("/update/default")
    public CommonResponse<LicenceDTO> updateDefault(@RequestBody @Validated(value = {Default.class, Update.class}) LicenceDTO licenceDTO) {
        return CommonResponse.success(licenceDTO);
    }

    /**
     * 参数为 List<T> 时, 要校验 T, 必须将 List 转换为对象的子属性.
     *     抛出异常 NotReadablePropertyException, 待处理信息
     * @param licences list
     * @return resp
     */
    @PostMapping("/updateList")
    public CommonResponse<List<LicenceDTO>> updateList(@RequestBody @Validated(Update.class) ValidationList<LicenceDTO> licences) {
        return CommonResponse.success(licences);
    }

}
